package the_Crash;


import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;

public class StreetCell extends BaseSimEntity {
	
	public final boolean CanTurnRight; 
	public final boolean CanTurnLeft;
	
	private boolean CanBeUsed;
	
	private Direction Heading;


	public StreetCell(GridPoint location, boolean canTurnRight,
			boolean canTurnLeft, boolean canBeUsed, Direction heading,
			Grid<Object> grid, ContinuousSpace<Object> space) {
		super(location, grid, space);		
		CanTurnRight = canTurnRight;
		CanTurnLeft = canTurnLeft;
		CanBeUsed = canBeUsed;
		Heading = heading;		
	}


	public boolean isCanTurnRight() {
		return CanTurnRight;
	}


	public boolean isCanTurnLeft() {
		return CanTurnLeft;
	}


	public boolean isCanBeUsed() {
		return CanBeUsed;
	}


	public Direction getHeading() {
		return Heading;
	}
	

	//next cell in the street that shares the heading with the current cell
	public StreetCell getNextCell()
	{
		StreetCell nextCell=null;
		int xDiff=0;
		int yDiff=0;
		if(Heading.equals(Direction.North))
			yDiff=1;
		if(Heading.equals(Direction.South))
			yDiff=-1;
		if(Heading.equals(Direction.East))
			xDiff=1;
		if(Heading.equals(Direction.West))
			xDiff=-1;
		
		if(CityBuilder.withinGrid(location.getX()+xDiff, location.getY()+yDiff)){
			for(Object o: grid.getObjectsAt(location.getX()+xDiff, location.getY()+yDiff))
			{
				if(o instanceof StreetCell && ((StreetCell) o).Heading.equals(this.Heading))
					nextCell = (StreetCell)o;
			}
		}
		return nextCell;
	}
	
	//previous cell in the street that shares the heading with the current cell
	public StreetCell getPreviousCell()
	{
		StreetCell nextCell=null;
		int xDiff=0;
		int yDiff=0;
		if(Heading.equals(Direction.North))
			yDiff=-1;
		if(Heading.equals(Direction.South))
			yDiff=1;
		if(Heading.equals(Direction.East))
			xDiff=-1;
		if(Heading.equals(Direction.West))
			xDiff=1;
		
		for(Object o: grid.getObjectsAt(location.getX()+xDiff, location.getY()+yDiff))
		{
			if(o instanceof StreetCell && ((StreetCell) o).Heading.equals(this.Heading))
				nextCell = (StreetCell)o;
		}
		return nextCell;
	}
	
	//****************************************************************************
	/*
	 * Checks to see if there is another car or an accident on the street cell
	 */
	public boolean isOpen(){
		for(Object o : grid.getObjectsAt(this.location.getX(), this.location.getY())){
			if(o instanceof Car || o instanceof Accident){
				return false;
			}
		}
		return true;
	}
	
	@Override
	public String toString()
	{
		return Heading+"";
	}
	

	public double color(){
		if(this.Heading.equals(Direction.East)){
			return 0;
		} else if(this.Heading.equals(Direction.West)){
			return 2.5;
		}
		else if(this.Heading.equals(Direction.North)){
			return 5;
		} else {
			return 7.5;
		}
	}
	
	
	
	
	
}
